package com.android.jay.lib_commin_ui;

import android.annotation.SuppressLint;
import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Rect;
import android.util.AttributeSet;
import android.util.Log;
import android.view.MotionEvent;
import android.view.View;
import android.widget.TextView;


import com.android.jay.lib_commin_ui.utils.StatusBarUtil;

import androidx.annotation.Nullable;

/**
 * Created by wzp on 2020/2/14 0014.
 * <p>
 * 描述  ：todo
 */
public class CustomTextView extends View  {
    private Paint mPaint;
    private int mTextSize;
    private int mTextColor;
    private String mText;
    private int mTextMax;


    public CustomTextView(Context context) {
        this(context, null);
    }

    public CustomTextView(Context context, @Nullable AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public CustomTextView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        TypedArray array = context.obtainStyledAttributes(attrs, R.styleable.CustomTextView);
        mText = array.getString(R.styleable.CustomTextView_customText);
        mTextColor = array.getColor(R.styleable.CustomTextView_customTextColor, Color.BLACK);
        mTextSize = array.getDimensionPixelSize(R.styleable.CustomTextView_customTextSize,
                StatusBarUtil.dip2px(context, 15));
        mTextMax = array.getInt(R.styleable.CustomTextView_customTextMax, 12);
        if (mText.length() > mTextMax) {
            mText = mText.substring(0, mTextMax) + "...";
        }
        array.recycle();
        mPaint = new Paint();
        mPaint.setAntiAlias(true);
        mPaint.setTextSize(mTextSize);
        mPaint.setColor(mTextColor);

    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
//        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
        /**
         * MeasureSpec.AT_MOST : 在布局中指定了wrap_content
         * MeasureSpec.EXACTLY : 在不居中指定了确切的值  100dp   match_parent  fill_parent
         * MeasureSpec.UNSPECIFIED : 尽可能的大,很少能用到，ListView , ScrollView 在测量子布局的时候会用UNSPECIFIED
         */
        //获取 模式
        int widthMode = MeasureSpec.getMode(widthMeasureSpec);
        int heightMode = MeasureSpec.getMode(heightMeasureSpec);

        //获取宽高
        int width = MeasureSpec.getSize(widthMeasureSpec);
        int height = MeasureSpec.getSize(heightMeasureSpec);
        //是warpcontent 则需要获取真实的宽高
        if (widthMode == MeasureSpec.AT_MOST) {
            @SuppressLint("DrawAllocation") Rect bounds = new Rect();
            mPaint.getTextBounds(mText, 0, mText.length(), bounds);
            width = bounds.width() + getPaddingLeft() + getPaddingRight();
            Log.e("CustomTextView", "width ==" + width);
        }

        if (heightMode == MeasureSpec.AT_MOST) {
            @SuppressLint("DrawAllocation") Rect bounds = new Rect();
            mPaint.getTextBounds(mText, 0, mText.length(), bounds);
            height = bounds.height() + getPaddingTop() + getPaddingBottom();
            Log.e("CustomTextView", "height ==" + height);
        }

        //设置宽高
        setMeasuredDimension(width, height);


    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        Paint.FontMetricsInt fm = mPaint.getFontMetricsInt();
        int dy = (fm.bottom - fm.top) / 2 - fm.bottom;
        //中心线
        int baseLine = getHeight() / 2 + dy;
        canvas.drawText(mText, getPaddingLeft(), baseLine, mPaint);
    }

    @SuppressLint("ClickableViewAccessibility")
    @Override
    public boolean onTouchEvent(MotionEvent event) {
        Log.e("TAG", event.getAction() + "111");
        switch (event.getAction()) {
            case MotionEvent.ACTION_DOWN:
                // 手指按下
                Log.e("TAG", "手指按下");
                break;

            case MotionEvent.ACTION_MOVE:
                // 手指移动
                Log.e("TAG", "手指移动");
                break;

            case MotionEvent.ACTION_UP:
                // 手指抬起
                Log.e("TAG", "手指抬起");
                break;
            default:
                break;
        }
        //要返回true 要不只能接收到 手指按下的指令
        return true;

    }


}
